package com.smart.framework.helper;


import com.smart.framework.annotation.Inject;
import com.smart.framework.util.ArrayUtils;
import com.smart.framework.util.CollectionUtils;
import com.smart.framework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * <p><b>Description:</b>  TODO
 * <p><b>Company:</b>
 *
 * @author created by Jesse Xu at 15:03 on 2019/9/23
 * @version V0.1
 * @classNmae IocHelper
 */
public class IocHelper {

    static {
        Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap();
        if(!CollectionUtils.isEmpty(beanMap)){
            for (Map.Entry<Class<?>, Object> beanEntry: beanMap.entrySet()) {
                Class beanClass =  beanEntry.getKey();
                Object beanInstance =  beanEntry.getValue();
                Field[] fields = beanClass.getDeclaredFields();
                if(!ArrayUtils.isEmpty(fields)){
                    for (Field beanField:fields){
                        if(beanField.isAnnotationPresent(Inject.class)){
                            Class<?> beanFieldClass = beanField.getType();
                            Object beanFieldInstance = beanMap.get(beanFieldClass);
                            if(beanFieldInstance!=null){
                                ReflectionUtil.setField(beanInstance,beanField,beanFieldInstance);
                            }

                        }
                    }
                }

            }
        }

    }
}
